
let mainBox = document.querySelector(".main-box");

function list() {

    ajax({
        url: "http://chst.vip:1234/api/getcategorytitle",
        success(res) {

            let html = "";
            res = res.result;
            // console.log(res);
            res.forEach(item => {
                html += `
                <div>
                    <h3>${item.title}</h3>
                    <a href="#"><img src="./../images/xia.png" alt=""></a>
                </div>
                <ul id="content">
                    
                </ul>
                    `
                mainBox.innerHTML = html;
            });
            let mainBoxs = mainBox.children;

            let arr = [...mainBoxs];
            // console.log(arr);

            let j = 0;//记录点击次数
            for (let i = 0; i < arr.length; i++) {
                if (i % 2 === 0) {
                    arr[i].onclick = function () {
                        j++;
                        // console.log(this);
                        if (j % 2 === 1) {

                            this.nextElementSibling.style.display = "block";

                            // console.log(this.nextElementSibling);
                            let content = mainBoxs[i + 1];
                            // console.dir(content);
                            ajax({
                                url: "http://chst.vip:1234/api/getcategory",
                                success(res) {
                                    let html = "";
                                    res = res.result;
                                    // console.log(res);
                                    res.forEach(item => {
                                        html += `
                                         <li categoryid=${item.categoryId}><a href="#">${item.category}</a></li>
                                        `
                                        // console.log(item.category);
                                    });
                                    // console.log(content);
                                    content.innerHTML = html;

                                    [...content.children].forEach(li => {
                                        li.onclick = function () {
                                            let categoryid = this.getAttribute("categoryid")
                                            console.log(categoryid);
                                            location.href = `../pages/brand.html?categoryid=${categoryid}`
                                        }
                                    })
                                },
                                data: {
                                    titleid: i / 2
                                },
                                error(err, xhr) {
                                    console.log(err);
                                }
                            })
                        } else {
                            this.nextElementSibling.style.display = "none";
                        }
                    }
                }
            }
        }
    })
}

list()
